$(function(){
	var service = new EbeimService();
    var jform = $("#j-form"),
        jsubmit = $("#j-submit");
    
    //占位符初始化
    plugin.placeHolder({
        con: $(".j-placeHolder")
    });
    
    //表单验证初始化
    jform.find(".j-formcheck").bind("blur", function() {
        var t = $(this);
        var name = t.attr("data-name");
        formCheck(jform,name);
    }).bind('focus',function() {
        var t = $(this);
        var error = t.siblings('.error');
        error.hide();
    });;
    
    //表单提交
    jform.bind("submit", function(){
        if (formCheck(jform)) {
            jsubmit.attr("disabled", "disabled");
            
            var data = jform.serialize();
            data = data + "&token=tokennawang";
            service.getForgetUser(data, function(json){
                if(json.code == 200){
                	window.location.href = "/web/html/forgetPwd.html?username="+json.obj.username+"&userId="+json.obj.id+"&mobile="+json.obj.mobile;
                }else{
                    jsubmit.removeAttr("disabled");
                    plugin.msgtip({type: "i",msg: json.desc});
                }
            })
        }
        return false;
    });
})

// 表单验证
var formCheck = function(form,name) {

	// 显示错误信息
    var showError = function(obj, msg) {
        var error = obj.siblings('.error');
        msg = msg + "<i></i>"
        error.html(msg);
        error.show();
    };

    // 隐藏错误信息
    var hideError = function(obj) {
        var error = obj.siblings('.error');
        error.hide();
    };

    // 数据验证对象
    var dataCheck = {
    	username: {
            obj: form.find("[data-name='username']"),
            fn: function() {
                var val = this.obj.val();
                if (plugin.checkFn.isNull(val)) {
                    showError(this.obj,"请输入用户名");
                    return false;
                } else {
                    hideError(this.obj);
                    return true;
                }
            }
        }
    };

    if (name && dataCheck[name]) {
        return dataCheck[name].fn();
    } else {
        var errLen = 0;
        for (var i in dataCheck) {
            if (!dataCheck[i].fn()) {
                errLen++;
            }
        }
        if (errLen > 0) {
            return false;
        }
        return true;
    }
}